package co.dreamapps.cyk.modelo;

import java.io.*;
import java.util.*;

public class Producciones
{
	public ArrayList<ArrayList<String>> arrayProducciones; 
	public ArrayList<String> arrayStringCadena;        
	private String stringGramatica;
	
	public Producciones(String stringGramatica)
	{
		this.stringGramatica = stringGramatica;
		arrayProducciones = new ArrayList<ArrayList<String>>();
		arrayStringCadena = new ArrayList<String>();
	}	

	public static void CopyArrayList(ArrayList<String> arrayDestino, ArrayList<String> arrayOrigen)
	{		//Copiando el arrayList
		for(int i = 0; i < arrayOrigen.size(); i++)
		{
			arrayDestino.add(arrayOrigen.get(i));
		}
	}
	
	
	public void leerEntradas(boolean printOutput)
	{
		try
		{
			ArrayList<String> arrayEntradaString = new ArrayList<String>();
			File inputFile = new File(this.stringGramatica);
			FileReader in = new FileReader(inputFile);
			
			int c;
			int contadorProducciones = 0;
			while ((c = in.read()) != -1)
			{
				//elimina la palabra Producciones:
				if (contadorProducciones > 12)
				{
					if (!(c == '\r' || c == '-' || c == '>'))
					{
						arrayEntradaString.add(Character.toString((char)c));
					}
				}
				contadorProducciones++;
			}
			in.close();
			
			int posicion = arrayEntradaString.lastIndexOf(":");			

			for (int i = posicion + 1; i < arrayEntradaString.size(); i++)
			{
				if(Character.isLetter(arrayEntradaString.get(i).charAt(0)))
				{
					arrayStringCadena.add((String)arrayEntradaString.get(i));
				}
			}

			for (int i = arrayEntradaString.size() - 1; i > posicion - 7; i--)
			{
				arrayEntradaString.remove(i);
			}
			
			
			String stringSaltoLinea = new String("\n");
			String stringSeparadorProducciones = new String("|");
			contadorProducciones = 0;

			if(Character.isLetter(arrayEntradaString.get(0).charAt(0)) == false)
			{
				arrayEntradaString.remove(0);
			}
			for (int i = 0; i < arrayEntradaString.size(); i++)
			{
				
				if (arrayEntradaString.get(i).equals(stringSaltoLinea))
				{
					contadorProducciones++;
				}
			}
		
			
			ArrayList<String> arrayStringTemporal = new ArrayList<String>();
			int j = 0;
			for(int i = 0; i < contadorProducciones; i++)
			{
				while(!arrayEntradaString.get(j).equals(stringSaltoLinea))
				{
					if(!arrayEntradaString.get(j).equals(stringSeparadorProducciones))
					{
						String temp = (String)arrayEntradaString.get(j);
						if(temp.equals("["))
						{
							temp = "";
							while(!arrayEntradaString.get(++j).equals("]"))
							{
								temp += arrayEntradaString.get(j);
							}
							arrayStringTemporal.add(temp);
							j++; 
						}
						else 
						{
							arrayStringTemporal.add(temp);
							j++;
						}
					}
					else
					{
					
						ArrayList<String> temp = new ArrayList<String>();
						CopyArrayList(temp, arrayStringTemporal);
						arrayProducciones.add(temp);
						arrayStringTemporal.clear();
						ArrayList rs = arrayProducciones.get(arrayProducciones.size() - 1); 
						arrayStringTemporal.add((String)(rs.get(0)));						
						j += 1; 
					}
				}
				
				if(!arrayStringTemporal.isEmpty())
				{
					ArrayList<String> temp = new ArrayList<String>();
					CopyArrayList(temp, arrayStringTemporal);
					arrayProducciones.add(temp);
					arrayStringTemporal.clear();
				}
				

				if (arrayEntradaString.get(j).equals(stringSaltoLinea) && (j != arrayEntradaString.size() - 1))
				{
					j++;
				}
			}
			
			if(printOutput)
			{
				imprimirReglas(arrayProducciones);
				imprimirCadena();
			}
			
		}
		catch (FileNotFoundException e)
		{
			System.out.println("FileNotFoundException:" + e);
		}
		catch (IOException e)
		{
			System.out.println("IOException:" + e);
		}
	}
	
	private void imprimirReglas(ArrayList R)
	{
		System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<ENTRADAS DEL PROGRAMA>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		System.out.println("-------------------------------------------------------------------------------");
		System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Gramatica G>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		System.out.println("- Se escribe cada una de las producciones de manera independiente:");
		for (int i = 0; i < R.size(); i++)
		{   System.out.print("  ("+i+") ");
		    
			ArrayList value = new ArrayList();
			value = (ArrayList)R.get(i);
			for (int j = 0; j < value.size(); j++)
			{
				if(j==0){
					System.out.print(value.get(j) + "->");
				}else{
				    System.out.print(value.get(j) + "");
				}
			}
			System.out.println();
		}
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
	}
	
	
	private void imprimirCadena()
	{

		System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Cadena w>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		System.out.print("  w= ");
		for (int i = 0; i < arrayStringCadena.size(); i++)
		{
			System.out.print(arrayStringCadena.get(i) + " ");
		}
		System.out.println();
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

	}
	

}
